package com.gla.controller;

import com.github.pagehelper.PageInfo;
import com.gla.commom.Result;
import com.gla.dto.OrderInfoDTO;
import com.gla.dto.OrderVO;
import com.gla.entity.Order;
import com.gla.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("page")
    public Result page(OrderVO listQuery) {
        List<Order> list = orderService.page(listQuery);
        PageInfo pageInfo = new PageInfo(list);
        return Result.succeed(pageInfo);
    }

    @PutMapping("")
    public Result update(@RequestBody Order order) {
        orderService.update(order);
        return Result.succeed("更新订单成功！");
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id) {
        orderService.delete(id);
        return Result.succeed("删除成功");
    }

    /**
     * 获取订单详情(包括订单内商品)
     * @param id
     * @return OrderInfoDTO
     */
    @GetMapping("detail/{id}")
    public Result getDetail(@PathVariable Integer id) {
        OrderInfoDTO order = orderService.getDetail(id);
        return Result.succeed(order);
    }

    /**
     * 获取待处理订单
     */
    @GetMapping("stateOrder/{state}")
    public Result getPendingOrder(@PathVariable Integer state){
        List<Order> list = orderService.getPendingOrder(state);
        return Result.succeed(list);
    }

    @GetMapping("pendingNumber")
    public Result getPendingNumber(){
        List<Order> list = orderService.getPendingOrder(1);
        List<Order> list2 = orderService.getPendingOrder(-2);
        return Result.succeed(list.size() + list2.size());
    }

}
